# coding=utf-8

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner import MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode
from datetime import datetime
import sys,os.path
sys.path.append(os.path.split(os.path.realpath(__file__))[0])
import util,traceback,login

'''
nearby_load_more (230,1180,470,1260)
usr_info (60,1200,300,1300)
add_confirm (250,70,450,150)
add_confirm_1 (30, 300, 200, 30)
'''


def  addFriends(device, easy_device, account, password, imgMap, outputPath):
    try:
        login.login(device, easy_device, "2935219538", "abc1234", imgMap, outputPath)
        MonkeyRunner.sleep(3)
        util.pressById_1(device, 'id/lebasv', "lebasv_0", 10, 0)
        if util.sameAs(device, 2, (260,800,200,40), "near_people", outputPath, imgMap, False):
            util.pressById_1(device, 'id/content', "content_0_3_3", 0, 3, 3)
            MonkeyRunner.sleep(3)
        
        z=0       
        for x in xrange(1,5):
            util.printLog("------------- Add friend " + str(x) + " -------------")
            nearPeople=util.getChildView(device, 'id/content', 0, 1, 0, 1+z)
            nearPeopleButton = HierarchyViewer.getAbsoluteCenterOfView(nearPeople)
            device.touch(nearPeopleButton.x, nearPeopleButton.y, "DOWN_AND_UP")
            # pressById_1(device, 'id/grid_list_view', 1+z)
            MonkeyRunner.sleep(2)
            if util.sameAs(device, 3, (60,1200,240,70), "usr_info", outputPath, imgMap, False):
                util.pressById_1(device, 'id/btns', "btns_0", 0)
                if util.sameAs(device, 3, (250,70,200,80), "add_verify", outputPath, imgMap, False):                    
                    if not util.sameAs(device, 1, (30, 300, 200, 50), "add_verify", outputPath, imgMap, False):
                        util.press(device, 'KEYCODE_BACK')
                        util.press(device, 'KEYCODE_BACK')
                        util.press(device, 'KEYCODE_BACK')
                    else:
                        util.pressById_1(device, 'id/rlCommenTitle', "rlCommenTitle_7_0", 7)
                        if util.sameAs(device, 3, (250,70,200,80), "add_confirm", outputPath, imgMap, False):
                            util.type(device, "hello_" + datetime.now().strftime('%y-%m-%d_%H%M%S'))
                            util.pressById_1(device, 'id/rlCommenTitle', "rlCommenTitle_7_0", 7)
                            MonkeyRunner.sleep(2)
                            if util.sameAs(device, 3, (250,70,200,80), "add_confirm", outputPath, imgMap, False):
                                util.press(device, 'KEYCODE_BACK')
                                util.press(device, 'KEYCODE_BACK') 
                                util.press(device, 'KEYCODE_BACK')
                                util.press(device, 'KEYCODE_BACK') 
                                break
                elif util.sameAs(device, 3, (250,70,200,80), "add_confirm", outputPath, imgMap, False):
                    util.type(device, "hello_" + datetime.now().strftime('%y-%m-%d_%H%M%S'))
                    util.pressById_1(device, 'id/rlCommenTitle', "rlCommenTitle_6_0", 6) 
                    MonkeyRunner.sleep(2)
                    if util.sameAs(device, 3, (250,70,200,80), "add_confirm", outputPath, imgMap, False):
                        util.press(device, 'KEYCODE_BACK')
                        util.press(device, 'KEYCODE_BACK')
                else:
                    util.press(device, 'KEYCODE_BACK')
                    util.press(device, 'KEYCODE_BACK')                  
            else:
                util.press(device, 'KEYCODE_BACK')
            MonkeyRunner.sleep(2)
            if x == 1:
                y = nearPeopleButton.y
                z = x;
            else:
                device.drag((50, nearPeopleButton.y), (50, y), 1, 10)
                MonkeyRunner.sleep(1)
            MonkeyRunner.sleep(2)
            if util.sameAs(device, 1, (230,1180,240,80), "nearby_load_more", outputPath, imgMap, False):
                util.easyTouch(easy_device, 'id/morebtnFooter')
                MonkeyRunner.sleep(1)
        MonkeyRunner.sleep(1)
        util.press(device, 'KEYCODE_BACK')
    except Exception, e:
        util.printLog("[Add friend exception] " + str(e))
        util.printLog("[Detail log] " + str(traceback.format_exc()))
        device.shell('am start -a android.intent.action.MAIN -n com.tencent.mobileqq/.activity.SplashActivity')
    MonkeyRunner.sleep(1)
    util.pressById_1(device, 'id/tabs', "tabs_3", 3, 2)
    util.pressById_1(device, 'id/accountSetting', "accountSetting_1", 1)
    device.drag((50, 900), (50, 100), 1, 10)
    MonkeyRunner.sleep(2)
    util.easyTouch(easy_device, 'id/logoutBtn',) 
    #util.easyTouch(easy_device, 'id/action_sheet_button')
    util.pressByIdRelative(device, "id/btnLayout", 50, 2)

def main():
    device = MonkeyRunner.waitForConnection()
    easy_device = EasyMonkeyDevice(device)
    imgPath = str(os.path.split(os.path.realpath(__file__))[0]) + os.sep + ".." + os.sep + "res" + os.sep + "image"
    outputPath = str(os.path.split(os.path.realpath(__file__))[0]) + os.sep + ".."  + os.sep + "output"  + os.sep + "image"
    imgMap = util.loadImage(imgPath)
    addFriends(device, easy_device, "2935219538", "abc1234", imgMap, outputPath)

if __name__ == '__main__':
    main()